<template>
  <div class="common-layout">
    <el-header style="height: 100%; width: 1000px; margin: auto">
      <el-icon class="collapseIcon" style="font-size: 26px" @click="tCollapse"
        ><Fold
      /></el-icon>

      <el-breadcrumb :separator-icon="ArrowRight" style="margin-left: 20px">
        <el-breadcrumb-item :to="{ path: '/' }">主页</el-breadcrumb-item>
        <el-breadcrumb-item :to="{ path: $route.path }">{{
          $route.meta.name
        }}</el-breadcrumb-item>
      </el-breadcrumb>

      <div
        style="
          flex: 1;
          width: 0;
          display: flex;
          align-items: center;
          justify-content: flex-end;
        "
      >
        <el-icon style="font-size: 26px" @click="toggleFullScreen"
          ><FullScreen
        /></el-icon>
        <el-dropdown placement="bottom-end">
          <div style="display: flex; align-items: center; cursor: default">
            <img
              src="@/assets/logo.jpg"
              alt=""
              style="width: 40px; height: 40px"
            />
            <span>管理员</span>
          </div>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item @click.native="$router.push('/user')"
                >个人中心</el-dropdown-item
              >
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
    </el-header>
    <el-container style="height: 100%; width: 1000px; margin: auto">
      <el-aside
        :style="{ width: asideWidth }"
        style="
          height: 50%;
          background-color: #e65c75;
          position: relative;
          top: 78px;
        "
      >
        <div
          style="
            height: 60px;
            color: white;
            display: flex;
            align-items: center;
            justify-content: center;
          "
        >
          <img
            src="@/assets/logo.png"
            style="width: 40px; height: 40px; margin-right: 10px"
          />
          <span class="loge-title" v-show="!isCollapse"><h5>个人中心</h5></span>
        </div>

        <el-menu
          :collapse="isCollapse"
          :collapse-transition="false"
          background-color="#001529"
          text-color="#646464"
          style="border: none"
          router
          :default-active="$route.path"
          class="el-menu-vertical-demo"
          @open="handleOpen"
          @close="handleClose"
          active-text-color="#001529"
        >
          <el-menu-item
            index="/"
            style="height: 100%; background-color: #ffffff"
          >
            <el-icon><House /></el-icon>
            <span>首页</span>
          </el-menu-item>

          <el-menu-item
            index="/user"
            style="height: 50%; background-color: #ffffff"
          >
            <el-icon><document /></el-icon>
            <span>订单管理</span>
          </el-menu-item>
          <el-menu-item
            index="/user"
            style="height: 50%; background-color: #ffffff"
          >
            <el-icon><User /></el-icon>
            <span>用户管理</span>
          </el-menu-item>
          <el-menu-item
            v-if="isAdmin"
            index="/admin"
            style="height: 50%; background-color: #ffffff"
          >
            <el-icon><User /></el-icon>
            <span>管理员页面</span>
          </el-menu-item>
          <el-menu-item
            index="4"
            style="height: 50%; background-color: #ffffff"
          >
            <el-icon><Setting /></el-icon>
            <span>设置</span>
          </el-menu-item>
        </el-menu>
      </el-aside>

      <el-container>
        <el-main><router-view @update:user="updateUser" /></el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script setup>
import { useRouter } from "vue-router";
import { ArrowRight } from "@element-plus/icons-vue";
import { onMounted, ref } from "vue";
import axios from "axios";
import "@/assets/iconfont/iconfont.css";

const router = useRouter();
const isCollapse = ref(false);
const asideWidth = ref("200px");
const isAdmin = ref(false);

const tCollapse = () => {
  isCollapse.value = !isCollapse.value;
  asideWidth.value = isCollapse.value ? "64px" : "200px";
};

const handleOpen = (key, keyPath) => {
  console.log(key, keyPath);
};

const handleClose = (key, keyPath) => {
  console.log(key, keyPath);
};

const toggleFullScreen = () => {
  if (!document.fullscreenElement) {
    document.documentElement.requestFullscreen();
  } else {
    if (document.exitFullscreen) {
      document.exitFullscreen();
    }
  }
};

onMounted(() => {
  axios
    .get("http://localhost:8000/usertype", { withCredentials: true })
    .then((response) => {
      if (response.data.isAdmin) {
        isAdmin.value = true;
      }
    });

  const currentPath = window.location.pathname;
  if (currentPath.includes("../views/manager/admin")) {
    isAdmin.value = true;
  }
});
</script>

<style scoped>
.el-menu--inline .el-menu-item {
  background-color: #e65c75;
}
/* .el-sub-menu__title {
   color: #001121   ;
           } */
.el-menu-item:hover,
.el-sub-menu__title:hover {
  color: #e65c75;
}
.el-sub-menu__title:hover i {
  color: #e65c75;
}
.el-menu-item.is-active {
  background-color: #fff0f5;
}
.el-sub-menu__title.is-active {
  background-color: #fff0f5;
}
.el-sub-menu:hover .el-sub-menu__title {
  background-color: #fff0f5;
}
.el-menu-item:hover {
  background-color: #fff0f5;
}
.el-aside {
  transition: all 0.3s;
  box-shadow: 2px 0 6px rgb(0 21 41 / 35%);
}

.loge-title {
  margin-left: 5px;
  font-size: 20px;
  transition: all 0.5s;
}
.el-header {
  box-shadow: 2px 0 6px rgb(0 21 41 / 35%);
  display: flex;
  align-items: center;
}
</style>
